package com.lvtu.monitor.quartz;

import java.lang.reflect.Method;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.quartz.DisallowConcurrentExecution;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.PersistJobDataAfterExecution;
import org.springframework.scheduling.quartz.QuartzJobBean;

import com.lvmama.com.lvtu.common.spring.SpringBeanProxy;
import com.lvtu.monitor.utils.Constants;

/**
 * @Title: MonitorDetailQuartzJobBean.java
 * @Package com.lvtu.monitor.quartz
 * @Description: TODO
 * @author dengcheng
 * @date 2015-5-27 上午9:50:15
 * @version V1.0.0
 */
@PersistJobDataAfterExecution
@DisallowConcurrentExecution
// 不允许并发执行
public class MonitorDetailQuartzJobBean extends QuartzJobBean {
	protected final Log logger = LogFactory.getLog(getClass());
	private String targetObject;
	private String targetMethod;

	public void setTargetObject(String targetObject) {
		this.targetObject = targetObject;
	}

	public void setTargetMethod(String targetMethod) {
		this.targetMethod = targetMethod;
	}

	@Override
	protected void executeInternal(JobExecutionContext context)
			throws JobExecutionException {
		Boolean quartzEnabled = Boolean.valueOf(Constants.getInstance()
				.getValue("quartz.enabled"));
		if (!quartzEnabled) {
			logger.info("quartz is not enable");
			return;
		}
		try {
			logger.info("execute [" + targetObject + "] at once>>>>>>");
			Object otargetObject = SpringBeanProxy.getBean(targetObject);
			Method m = null;

			try {
				m = otargetObject.getClass().getMethod(targetMethod,
						new Class[] { JobExecutionContext.class });
				m.invoke(otargetObject, new Object[] { context });
			} catch (SecurityException e) {
				logger.error(e);
			} catch (NoSuchMethodException e) {
				logger.error(e);
			}
		} catch (Exception e) {
			throw new JobExecutionException(e);
		}
	}

}
